/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package sys.donto.prontuario;

import java.util.List;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import sys.donto.paciente.Paciente;
import sys.donto.util.GenericDAO;
import sys.donto.util.HibernateUtil;

/**
 *
 * @author JOABB
 */
public class ProntuarioDAO extends GenericDAO<Prontuario>{
     public ProntuarioDAO() {
        super(Prontuario.class);
    }
    public List <Prontuario> pesquisaDescricao(String Nome){
        setSessao(HibernateUtil.getSessionFactory().openSession());
        setTransacao(getSessao().beginTransaction());
        
        List <Prontuario> prontuarios = (List<Prontuario>) getSessao().createCriteria(Prontuario.class).
                add(Restrictions.ilike("prontuario", Nome, MatchMode.ANYWHERE)).
                addOrder(Order.asc("prontuario")).list();
        
        getSessao().close();
        return prontuarios;
    }
    
    public Prontuario pesquisaId(int id){
        setSessao(HibernateUtil.getSessionFactory().openSession());
        setTransacao(getSessao().beginTransaction());
        
        Prontuario prontuario = (Prontuario) getSessao().createCriteria(Prontuario.class).
                add(Restrictions.eq("id", id)).uniqueResult();
        
        getSessao().close();
        return prontuario;
    }
    
    
    public void salvar(Prontuario pront){
        if(pront.getId()==0){
            adicionar(pront);
        } else {
            atualizar(pront);
        }
    }
    
    
}
